package com.ruoyi.common.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.pagination.Paging;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.pagination.PageInfo;
import com.ruoyi.common.enums.ShoppingCartStatusEnums;
import com.ruoyi.common.param.ShoppingCartParam;
import com.ruoyi.common.utils.satoken.StpWxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.mapper.business.ShoppingCartMapper;
import com.ruoyi.common.domain.business.ShoppingCart;
import com.ruoyi.common.service.business.ShoppingCartService;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车Service业务层处理
 *
 * @author Song
 * @date 2024-10-29
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 查询购物车列表
     *
     * @param shoppingCart 购物车
     * @return 购物车
     */
    public Paging<ShoppingCart> pageShoppingCart(ShoppingCartParam shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
        Page<ShoppingCart> page = new PageInfo<>(shoppingCart);

        IPage<ShoppingCart> iPage = this.page(page, wrapper);
        // 最终返回分页对象
//        IPage<ShoppingCartVo> resultPage = iPage.convert(item -> {
//            ShoppingCartVo vo = new ShoppingCartVo();
//            BeanUtil.copyProperties(item, vo);
//            return vo;
//        });
        return new Paging(iPage);
    }

    public Object countLocal() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, StpWxUtils.getLoginId());
        wrapper.eq(ShoppingCart::getStatus, ShoppingCartStatusEnums.SHOPPING.getKey());
        return count(wrapper);
    }

    public Object getTotalPrice() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, StpWxUtils.getLoginId());
        wrapper.eq(ShoppingCart::getStatus, ShoppingCartStatusEnums.SHOPPING.getKey());
        List<ShoppingCart> list = list(wrapper);
        return list.stream().map(ShoppingCart::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}



